﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.SqlClient;
using moviecentro;
using CruzSur.Models;
using System.Data;

namespace CruzSur.Controllers
{
    public static partial class AppDa
    {
        public static int ExisteGPS(string Codigo)
        {
            System.Data.SqlClient.SqlParameter[] param = new System.Data.SqlClient.SqlParameter[0];

            return dbgestor.ListarSql2ScalarInt("select count(*) from  Vehiculo where Placa = '" + Codigo + "'", param); ;
        }

        public static void GPSInsertar(EncomiendaModel item)
        {

            dbsql.ClearSQL();
            dbsql.AddTable("gps");

            dbsql.AddField(SqlDbType.Int, "KM", item.ID);
            dbsql.AddField(SqlDbType.Int, "Velocidad", item.Codigo);
            dbsql.AddField(SqlDbType.VarChar, "PLACA", item.Destino);
                       

            dbsql.InsertNow();
        }

        public static List<EncomiendaModel> ListarGPSItem(string IDItem)
        {
            // ----- Lista C01
            List<EncomiendaModel> ListData = new List<EncomiendaModel>();

            System.Data.SqlClient.SqlParameter[] param = new System.Data.SqlClient.SqlParameter[1];
            param[0] = new SqlParameter("@ID_Viaje", SqlDbType.VarChar);
            param[0].Value = IDItem;

            SqlDataReader reader = dbgestor.ListarSql2("select top 15 * from gps where placa=  @ID_Viaje order by id desc ", param);
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    EncomiendaModel ListItem = new EncomiendaModel();
                    ListItem.ID = reader.GetInt32(reader.GetOrdinal("KM"));
                    ListItem.Codigo = reader["Velocidad"].ToString();
                    ListItem.Destino = reader["PLACA"].ToString();

                    ListData.Add(ListItem);
                }
            }
            reader.Close();

            return ListData;
        }
    }
}